﻿using System;
using System.Collections.Generic;
using System.Linq;
using FoodRaidersWebAPI.domainnew.Comment;
using FoodRaidersWebAPI.Models;

namespace FoodRaidersWebAPI.Service.Third
{
    public class ArticleCommentService
    {
        private readonly FoodContext context;

        public ArticleCommentService(FoodContext context)
        {
            this.context = context;
        }
        //id查询 不加载用户
        public ArticleComment findByID(int id)
        {
            return context.ArticleComments.FirstOrDefault(a => a.ArticleCommentID == id);
        }
        //id查询 全加载
        public ArticleComment findByIDFullLoad(int id)
        {
            var articleComment = findByID(id);
            context.Entry(articleComment).Reference(a => a.User).Load();
            return articleComment;
        }
        
        //添加评论
        public void addComment(int aid, int uid,string content)
        {
            context.ArticleComments.Add(new ArticleComment()
            {
                ArticleID = aid, Content = content, DateTime = DateTime.Now, UserID = uid
            });
            context.SaveChanges();
        }
        //返回指定aid的文章的所有评论id 按获赞数降序排列
        public List<int> findAllID(int aid)
        {
            var query = context.ArticleComments.Where(a => a.ArticleID == aid).OrderByDescending(a=>a.PraiseNumber).Select(a => a.ArticleCommentID);
            return query.ToList();
        }
    }
}